<?php
/**
 * Created by PhpStorm.
 * User: renyi
 * Date: 2017/4/7
 * Time: 下午14:48
 */

namespace Modules\Chat\Base;


use App\Helper\ErrorHelper;

trait BaseServiceMethod
{
    /**
     * 成功时返回的数据
     *
     * @param array  $data 返回的数据
     * @param string $msg  描述
     * @param string $error_code 状态值
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function apiSuccess($data, $msg = ErrorHelper::ERROR_OK_MESSAGE, $error_code = ErrorHelper::ERROR_OK)
    {
        if (!is_array($data)){
            $data = (object)$data;
        }

        return response()->json(
            [
                "error_code"    => $error_code,
                "error_message" => $msg,
                "data"          => $data,
            ], 200);
    }

    /**
     * 失败时返回的数据
     *
     * @param string $msg 描述
     * @param array  $data 返回的数据
     * @param string $error_code 状态值
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function apiError($msg = ErrorHelper::MESSAGE_FAILED, $data = [], $error_code = ErrorHelper::ERROR_FAILED)
    {
        return response()->json(
            [
                "error_code"    => $error_code,
                "error_message" => $msg,
                "data"          => (object)$data,
            ], 200);
    }
}
